package com.another.bidservice.service.impl;

import com.another.bidservice.entity.TCompanyUser;
import com.another.bidservice.dao.jpa.CompanyUserJPADao;
import com.another.bidservice.dao.mapper.CompanyUserDao;
import com.another.bidservice.service.ICompanyUserService;
import com.another.common.utils.common.DateUtil;
import com.another.common.utils.common.EmptyUtil;
import com.another.common.utils.common.MyException;
import com.another.common.utils.result.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class CompanyUserServiceImpl extends BaseServiceImpl implements ICompanyUserService {

    private Logger logger=Logger.getLogger(this.getClass());
    
    @Autowired
    private CompanyUserDao companyuserDao;
    
    @Autowired
    private CompanyUserJPADao companyuserJPADao;
    
    @Override
    public Result getList(TCompanyUser tCompanyUser) {
        String sql="select * from db_sys.vt_company_user";
        List<TCompanyUser> list=companyuserDao.getList(sql);
        if (EmptyUtil.isEmpty(list)) {
            return Result.success("没有查询到数据！", "");
        } else {
            return Result.success(list);
        }
    }

    
    @Override
    public Result insertList(List<TCompanyUser> tCompanyUsers) {
        int ret = companyuserDao.insertList(tCompanyUsers);
        if(ret==tCompanyUsers.size()){
            return Result.success("添加成功！","");
        }else{
            throw new MyException(101, "添加失败！");
        }
    }

    
    @Override
    public Result deleteList(List<Integer> rowid) {
        int ret = companyuserDao.deleteList(rowid);
        if(ret==rowid.size()){
            return Result.success("删除成功！","");
        }else{
            throw new MyException(101, "删除失败！");
        }
    }
    
    @Override
    public Result updateList(List<TCompanyUser> tCompanyUsers) {
        try {
            int ret = companyuserDao.updateList(tCompanyUsers);
            return Result.success("修改成功！","");
        }catch (Exception e){
            logger.info(e.getMessage(),e);
            throw new MyException(101, "修改失败！");
        }
    }
    
    
    @Override
    public Result insertCompanyUser(TCompanyUser tCompanyUser) {
        try {

            if (EmptyUtil.isEmpty(tCompanyUser.getCompanyId())) {
                return Result.error("请选择公司！");
            }
            if (EmptyUtil.isEmpty(tCompanyUser.getUserId())) {
                return Result.error("请选择用户！");
            }

            //查看用户是否已加入公司
            List<TCompanyUser> list = companyuserJPADao.findAllByCompanyIdAndUserId(tCompanyUser.getCompanyId(), tCompanyUser.getUserId());
            if (EmptyUtil.isNotEmpty(list)) {
                return Result.error("用户已经加入该公司,无需重复添加！");
            }

            tCompanyUser.setCreateId(tCompanyUser.getRequestUserId());
            tCompanyUser.setCreateDate(DateUtil.getNowDateByyyyyMMddHHmmss());
            companyuserJPADao.save(tCompanyUser);
            return Result.success("添加成功！");
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            throw new MyException(101, "添加失败！");
        }
    }

    @Override
    public Result updateCompanyUser(TCompanyUser tCompanyUser) {
        try {
            int ret = companyuserDao.updateCompanyUser(tCompanyUser);
            return Result.success("修改成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            throw new MyException(101, "修改失败！");
        }
    }

    @Override
    public Result deleteCompanyUser(TCompanyUser tCompanyUser) {
        try {


            if (EmptyUtil.isEmpty(tCompanyUser.getCompanyId())) {
                return Result.error("请选择公司！");
            }
            if (EmptyUtil.isEmpty(tCompanyUser.getUserId())) {
                return Result.error("请选择用户！");
            }
            int ret = companyuserDao.deleteCompanyUser(tCompanyUser);
            return Result.success("删除成功！", "");
        } catch (Exception e) {
            logger.info(e.getMessage(),e);
            throw new MyException(101, "删除失败！");
        }
    }
}



